<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="search">
        <el-form-item>
          <el-input v-model="search.key" placeholder="员工姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="queryData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.data" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="员工姓名" width="120" sortable>
      </el-table-column>
      <el-table-column prop="age" label="员工年龄" width="120" sortable>
      </el-table-column>
      <el-table-column prop="email" label="员工邮箱" width="220" sortable>
      </el-table-column>
      <el-table-column prop="headImage" label="员工头像" width="220" sortable>
      </el-table-column>
      <el-table-column prop="department.name" label="员工所属部门" width="180" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleSave(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="search.currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total"
          style="float: right">
      </el-pagination>
    </el-col>

    <!--新增/修改界面-->
    <el-dialog title="新增/修改" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="110px" ref="saveForm">
        <el-form-item label="员工姓名" prop="username">
          <el-input v-model="saveForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工年龄" prop="age">
          <el-input v-model="saveForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工邮箱" prop="email">
          <el-input v-model="saveForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工头像" prop="headImage">
          <el-input v-model="saveForm.headImage" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工所属部门">
          <el-cascader v-model="saveForm.department"
                       :options="deptTree"
                       :props="{
							   checkStrictly: true,
							   label:'name',
							   value:'id'
							}"
                       clearable></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>

export default {
  data() {
    return {
      employees: [],
      deptTree: [],
      search: {
        key: '',
        pageSize: 5,
        currentPage: 1
      },
      pageInfo: {
        data: [],
        total: 0
      },
      listLoading: false,
      sels: [],//列表选中列

      saveFormVisible: false,
      saveForm: {
        id: null,
        username: "",
        age: "",
        email: "",
        headImage: "",
        department: {
          id: null,
          name: "",
          path:""
        }
      }
    }
  },
  methods: {
    //获取员工例表
    getEmployees() {
      this.$http.post("/employee",this.search)
          .then(res => {
            res = res.data;
            if (res.success) {
              this.pageInfo = res.data;
            }
          })
          .catch(res => {
          })
    },
    //获取上级部门树
    getdeptTreeTh(){
      this.$http.get("/department/deptTreeTh")
          .then(res=>{
            res = res.data;
            if(res.success){
              this.deptTree = res.data
            }
          })
          .catch(res=>{})
    },
    //点击新增
    handleAdd() {
      this.saveFormVisible = true;
      this.saveForm = {
        id: null,
        username: "",
        age: "",
        email: "",
        headImage: "",
        department: {
          id: null,
          name: "",
          path: ""
        }
      };
      //获取上级部门树
      this.getdeptTreeTh();
    },
    //当每页条数发生变化
    handleSizeChange(val) {
      this.search.pageSize = val;
      this.getEmployees();
    },
    //当前页发生变化
    handleCurrentChange(val) {
      this.search.currentPage = val;
      this.getEmployees();
    },
    //点击查询
    queryData() {
      this.search.currentPage = 1;
      this.getEmployees();
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/employee/" + row.id)
            .then(res => {
              res = res.data;
              if (res.success) {
                this.$message({
                  type: "success",
                  message: "删除成功"
                })
              } else {
                this.$message.error("删除失败" + res.msg)
              }
              this.listLoading = false;
              this.queryData();
            })
            .catch(res => {
            });
      }).catch(() => {

      });
    },
    //点击显示新增/修改界面
    handleSave: function (index, row) {
      this.saveFormVisible = true;
      this.saveForm = row;
      var split = row.department.path.split("/");
      console.log(split);
      var data = [];
      for (let i = 1; i < split.length - 1; i++) {
        data.push(Number.parseInt(split[i]));
      }
      this.saveForm.department = data
      if (!row.department) {
        this.saveForm.department = {
          id: null,
          name: "",
          path: ""
        }
      }
      //调用查询管理员
      this.getEmployees()
      //获取上级部门树
      this.getdeptTreeTh();
    },
    //新增/修改
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {})
              .then(() => {
                //NProgress.start();
                let para = Object.assign({}, this.saveForm);
                var department = para.department;
                if (department.length >= 0) {
                  if (department.length == 0) {
                    para.department = {id: null, name: ""}
                  } else {
                    para.department = {id: department[department.length - 1], name: ""}
                  }
                }
                this.$http.put("/employee", para)
                    .then(res => {
                      this.saveFormVisible = false;
                      this.$message({
                        type: "success",
                        message: "保存成功"
                      })
                      this.queryData();
                    })
                    .catch(res => {
                    });
              });
        }
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/employee", ids)
            .then(res => {
              res = res.data;
              if (res.success) {
                this.$message({
                  type: "success",
                  message: "删除成功"
                })
              } else {
                this.$message.error("删除失败" + res.msg)
              }
              this.listLoading = false;
              this.queryData();
            })
            .catch(res => {
            });
      }).catch(() => {
      });
    }
  },
  mounted() {
    this.getEmployees();
  }
}

</script>

<style scoped>

</style>